package com.gotrade.demo;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * @author jason.tang
 * @create 2018-12-04 11:48
 * @description 服务端
 */
public class NettyServer {
    public static void main(String[] args) {
        ServerBootstrap bootstrap = new ServerBootstrap();

        NioEventLoopGroup bossGrp = new NioEventLoopGroup();
        NioEventLoopGroup workerGrp = new NioEventLoopGroup();

        bootstrap.group(bossGrp, workerGrp)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    protected void initChannel(NioSocketChannel socketChannel) throws Exception {
                        /** 指定连接数据读写逻辑 **/
                        // 1. 获取与这条连接相关的逻辑处理链（责任链模式）
                        ChannelPipeline pipeline = socketChannel.pipeline();
                        // 2. 添加一个逻辑处理器（负责读取客户端写来的数据）
                        pipeline.addLast(new FirstServerHandler());
                    }
                });
        bootstrap.bind("localhost",9099);
    }
}